package com.hitown.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


public class CopyUtil {

	public static Map<String, Object> copyToMap(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String name = (String) enumeration.nextElement();
			Object value = null;
			String[] arr = request.getParameterValues(name);
			if(arr != null && arr.length == 1){
				if(StringUtils.isNotBlank(arr[0])){
					value = arr[0];
					try {
						value = URLDecoder.decode(arr[0] , "utf-8");
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(arr != null && arr.length > 1){
				value = arr;
				value = arr[0];
				try {
					value = URLDecoder.decode(arr[0] , "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (value!=null) {
				value = arr[0];
				try {
					value = URLDecoder.decode(arr[0] , "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				map.put(name, value);
			}
		}
		return map;
	}
}
